package com.piece.mall.service;

import com.piece.core.framework.enums.sql.SqlKey;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.Order;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.util.collection.MapUtil;
import com.piece.core.framework.util.object.BeanUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.mall.model.PmsAttr;
import com.piece.mall.model.PmsAttrGroup;
import com.piece.mall.repository.PmsAttrGroupRepository;
import com.piece.mall.vo.AttrGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class PmsAttrGroupService extends BaseService<PmsAttrGroup, String> {

    @Autowired
    private PmsAttrGroupRepository pmsAttrGroupRepository;

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private PmsAttrService pmsAttrService;

    @PostConstruct
    public void initRepository() {
        setRepository(pmsAttrGroupRepository);
    }

    /**
     * 获取下一个序号
     */
    public Long findNextGroupSort() {
        String sql = "select ifnull(max(group_sort) + 1,1) from pms_attr_group";
        return jdbcTemplate.queryForObject(sql, Long.class);
    }

    /**
     * 查询属性分组关联的所有属性分页数据
     */
    public DataTable<PmsAttr> findRelationPage(QueryTable queryTable, Long groupId) {
        return pmsAttrService.findRelationPage(queryTable, groupId);
    }

    /**
     * 查询属性分组未关联的所有属性分页数据
     */
    public DataTable<PmsAttr> findUnRelationPage(QueryTable queryTable, Long groupId) {
        return pmsAttrService.findUnRelationPage(queryTable, groupId);
    }

    /**
     * 获取分类下所有分组及关联属性
     */
    public List<AttrGroupVo> getAttrGroupWithAttrs(Long catalogId, Integer attrType) {
        if (1 == attrType) {
            // 关联规格参数
            List<PmsAttrGroup> catalogGroups = pmsAttrGroupRepository.findByCondition("catalogId", catalogId,
                    Arrays.asList(new Order("groupSort", SqlKey.ASC)));
            return catalogGroups.stream().map(group -> {
                AttrGroupVo vo = new AttrGroupVo();
                try {
                    BeanUtil.copyProperties(group, vo, "catalogPath", "groupSort");
                } catch (Exception e) {
                }
                vo.setAttrs(pmsAttrService.getRelationAttr(group.getAttrGroupId(), attrType));
                return vo;
            }).collect(Collectors.toList());
        } else {
            // 关联销售属性
            AttrGroupVo vo = new AttrGroupVo();
            List<PmsAttr> attrs = pmsAttrService.findByCondition(MapUtil.builder(new HashMap<String, Object>())
                    .put("Q_attrType_S_EQ", attrType)
                    .put("Q_catalogId_L_EQ", catalogId).build());
            vo.setAttrs(attrs);
            return Arrays.asList(vo);
        }
    }
}
